package com.example.administrator.hospitalmap

import android.support.multidex.MultiDexApplication
import android.util.Log
import com.example.administrator.hospitalmap.entity.HospitalDetail
import com.example.administrator.hospitalmap.entity.HospitalSearchRes
import com.tangula.android.http.HttpBaseKotlin
import com.tangula.android.utils.ApplicationUtils
import com.tangula.android.utils.ToastUtils

class HostpitalMapApplication : MultiDexApplication() {

    /**
     * http loading status.
     * - 0: default
     * - 1: loading
     * - 2: loading success
     * - 3: loading response format not well
     * - 4: http error
     *
     */
    var loadingStatus = 0

    var loadingSuccessTm = 0L

    val latestQueryResult = mutableListOf<HospitalDetail>()

    lateinit var SEARCH_ACTIONS:Array<String>

    override fun onCreate() {
        super.onCreate()
        ApplicationUtils.APP = this
        HttpBaseKotlin.URL_PREFIX="http://pasarhits.com/nearbyplace/api-post"
        CUR_APP = this

        SEARCH_ACTIONS = resources.getStringArray(R.array.radius_list)

        loadHospitals (0){ _->
        }

    }

    fun loadHospitals(action:Int, callback:(hospitals:List<HospitalDetail>)->Unit){

        object:HttpBaseKotlin(){

            fun queryHospitials(){
                when{
                    loadingStatus in arrayOf(0,3,4)->{
                        getA(HttpBaseKotlin.URL_PREFIX, null, HospitalSearchRes::class.java, { res, _, _ ->
                            synchronized(latestQueryResult){
                            latestQueryResult.clear()
                            latestQueryResult.addAll(res?.rsbpjs?: listOf())
                            loadingSuccessTm = System.currentTimeMillis()
                            loadingStatus = 2
                            callback(res?.rsbpjs ?: listOf())
                            }
                        }, { resp, _ ->
                            synchronized(latestQueryResult){
                                latestQueryResult.clear()
                            }
                            ToastUtils.showToastLong("The search reslt's format is not well!")
                            Log.w("console", "recieve a wrong format resposne:${resp.body()}")
                        }, { ex, _ ->

                            synchronized(latestQueryResult){
                                latestQueryResult.clear()
                            }

                            ToastUtils.showToastLong("Can't connected network!")
                            Log.e("console", ex.localizedMessage, ex)
                        }
                        )
                    }
                    loadingStatus == 2 && (System.currentTimeMillis() - loadingSuccessTm)<60000->{
                        callback(latestQueryResult)
                    }
                    else->callback(latestQueryResult)
                }
            }
        }.queryHospitials()
    }

    companion object {
        @JvmStatic
        lateinit var CUR_APP:HostpitalMapApplication
    }

    fun convSearchActionIndex2SearchDistance(index:Int):Int{
        return when(index){
            1->10000
            2->10000
            3->30000
            4->50000
            5->80000
            6->100000
            else -> Int.MAX_VALUE
        }
    }

}